from flask import Flask, render_template, request, jsonify, redirect, url_for

app = Flask(__name__)

# 模拟用户数据库
users = {
    'admin': '123456',
    'user': 'password'
}


@app.route('/')
def index():
    return redirect(url_for('login'))


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        try:
            # 获取JSON数据
            data = request.get_json()
            username = data.get('username')
            password = data.get('password')

            # 验证用户
            if username in users and users[username] == password:
                return jsonify({
                    'success': True,
                    'message': '登录成功'
                }), 200
            else:
                return jsonify({
                    'success': False,
                    'message': '用户名或密码错误'
                }), 401
        except Exception as e:
            return jsonify({
                'success': False,
                'message': f'服务器错误: {str(e)}'
            }), 500

    # GET请求返回登录页面
    return render_template('login.html')


@app.route('/welcome')
def welcome():
    return render_template('welcome.html')


if __name__ == '__main__':
    app.run(debug=True)

